import 'package:flutter/material.dart';
import 'package:draw_window/basic.dart';

class VerticalEndure extends RegisterWidget{

  double width = 10.0;

  @override
  double get measure {
    return 3.0;
  }

  @override
  String get type {
    return "vertical-endure";
  }

  @override
  RegisterWidget get clone {
    return VerticalEndure();
  }

  @override
  void insert(Offset offset, Node parent) {
    if(offset.dx + width <= parent.startX + parent.availableWidth){
      var lw = offset.dx - parent.startX;
      var rw = parent.availableWidth - lw - width;
      parent.children.add([
        Node(type: "container",width: lw),
        Node(type: type,width: width),
        Node(type: "container",width: rw),
      ]);
      parent.minWidth = rw + width;
    }
  }

  @override
  GestureDragUpdateCallback get onHorizontalDragUpdate {
    return (DragUpdateDetails details){
       redraw((){
        var x = details.globalPosition.dx;
        final cw = x - node.startX;
        if(cw < 0){
          node.prev.width -= cw.abs();
          node.next.width += cw.abs();
        }
        else{
          node.prev.width += cw;
          node.next.width -= cw;
        }
      });
    };
  }

  @override
  void update(Vary vary, double value) {
    if(vary == Vary.width){

    }
  }
}




class VerticalCenterEndure extends RegisterWidget{

  double width = 10.0;

  @override
  double get measure {
    return 4.0;
  }

  @override
  String get type {
    return "vertical-center-endure";
  }

  @override
  RegisterWidget get clone {
    return VerticalCenterEndure();
  }

  @override
  void insert(Offset offset, Node parent) {
    if(parent.availableWidth >= width){
      var w = (parent.availableWidth - width) / 2;
      w = w < 0 ? 0 : w;
      parent.children.add([
        Node(type: "container",width: w),
        Node(type: type,width: width),
        Node(type: "container",width: w),
      ]);
    }
  }

  @override
  void update(Vary vary, double value) {

  }
}


class HorizontalEndure extends  RegisterWidget{

  double height = 10.0;

  @override
  double get measure {
    return 4.0;
  }

  @override
  String get type {
    return "horizontal-endure";
  }

  @override
  void insert(Offset offset, Node parent) {
    if(offset.dy + height <= parent.startY + parent.availableHeight){
      var lh = offset.dy - parent.startY;
      var rh = parent.availableHeight - lh - height;
      parent.children.add([
        Node(type: "container",height: lh),
        Node(type: type,height: height),
        Node(type: "container",height: rh),
      ]);
    }
  }

  @override
  RegisterWidget get clone {
    return HorizontalCenterEndure();
  }

  @override
  void update(Vary vary, double value) {

  }
}

class HorizontalCenterEndure extends  RegisterWidget{

  double height = 10.0;

  @override
  double get measure {
    return 4.0;
  }
  
  @override
  String get type {
    return "horizontal-center-endure";
  }

  @override
  void insert(Offset offset, Node parent) {
    if(parent.availableHeight >= height){
      var h = (parent.availableHeight - height) / 2;
      h = h < 0 ? 0 : h;
      parent.children.add([
        Node(type: "container",height: h),
        Node(type: type,height: height),
        Node(type: "container",height: h),
      ]);
    }
  }

  @override
  RegisterWidget get clone {
    return HorizontalCenterEndure();
  }

  @override
  void update(Vary vary, double value) {

  }
}

